package com.users.service.impl;

import com.commons.utils.MD5Tools;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.users.dao.UsersDao;
import com.users.dto.AdminLoginDto;
import com.users.entity.Users;
import com.users.service.UsersService;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author mis_wu
 * @date 2021/11/30 17:11
 */
@Service
@Transactional(rollbackFor = Exception.class)
//@Component
//@RabbitListener(queues = "admins-mq")
public class UsersServiceImpl implements UsersService {

    private static final Logger log=LoggerFactory.getLogger(UsersServiceImpl.class);

    @Resource
    private UsersDao usersDao;

//    @RabbitHandler
//    @RabbitListener(queues = "admins-mq")
    public void process(AdminLoginDto adminLoginDto) {
        log.info("receive_mq:{}",adminLoginDto.toString());
    }

    /**
     * 查询
     * @param page
     * @param size
     * @return
     */
    public PageInfo<Users> selectAllUsers(int page,int size) {
        PageHelper.startPage(page,size);
        return new PageInfo(usersDao.selectAllUsers());
    }

    /**
     * 用户注册
     * @param users
     * @return
     */
    public int saveUserInfo(Users users) {
        Users u = selectByloginId(users.getLoginId());
        if (ObjectUtils.isEmpty(u)){
            //登陆密码 加密
            String md5Pwd = MD5Tools.convertMD5(users.getLoginPwd());
            users.setLoginPwd(md5Pwd);
            return usersDao.saveUserInfo(users);
        }
        return 0;
    }

    /**
     * 短信登陆
     * @param phone
     * @return
     */
    public Users findByPhone(String phone) {
        return usersDao.findByPhone(phone);
    }

    public int delUserByLoginId(String loginid) {
        return 0;
    }

    public int insert(Users users) {
        return 0;
    }

    /**
     * 账号密码登陆
     * @param loginid
     * @return
     */
    public Users selectByloginId(String loginid) {
        return usersDao.selectByloginId(loginid);
    }

    /**
     * 修改用户信息
     * @param users
     * @return
     */
    public int updateUserByLoginId(Users users) {
        return usersDao.updateUserByLoginId(users);
    }
}
